package com.alks.common.utils.stringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author MS
 * @since 2025/3/1下午4:42
 */
public class SplitUtils {

    /**
     * 根据规则分割字符串
     * @param input 输入字符串
     * @return 分割后的字符串列表
     */
    public static List<String> splitString(String input) {
        // 去除空格
        String cleanedInput = input.replaceAll("\\s+", "");

        // 初始化分割结果数组
        String[] parts = new String[3];
        int splitCount = 0;

        // 第一次分隔符切割
        int firstDashIndex = cleanedInput.indexOf('-');
        if (firstDashIndex != -1) {
            char beforeFirstDash = cleanedInput.charAt(firstDashIndex - 1);
            char afterFirstDash = cleanedInput.charAt(firstDashIndex + 1);

            if (Character.isDigit(beforeFirstDash) && Character.isDigit(afterFirstDash)) {
                // 如果第一个 '-' 符号前后都是数字，视为一个整体
                int nextDashIndex = cleanedInput.indexOf('-', firstDashIndex + 1);
                if (nextDashIndex == -1) {
                    parts[splitCount++] = cleanedInput;
                } else {
                    parts[splitCount++] = cleanedInput.substring(0, nextDashIndex);
                    cleanedInput = cleanedInput.substring(nextDashIndex + 1);
                }
            } else {
                // 否则，在第一个 '-' 符号处分割
                parts[splitCount++] = cleanedInput.substring(0, firstDashIndex);
                cleanedInput = cleanedInput.substring(firstDashIndex + 1);
            }
        }
        // 第二次分隔符切割
        int secondDashIndex = cleanedInput.indexOf('-');
        if (secondDashIndex != -1 && splitCount < 2) {
            String beforeSecondDash = cleanedInput.substring(secondDashIndex - 2, secondDashIndex);
            if ("MM".equals(beforeSecondDash)) {
                // 如果第二个 '-' 符号前的部分是 "MM"，进行分割
                parts[splitCount++] = cleanedInput.substring(0, secondDashIndex); // 提取 "1.0MM"
                parts[splitCount++] = cleanedInput.substring(secondDashIndex + 1);
            } else {
                // 如果第二个 '-' 符号前的部分不是 "MM"，视为一个整体
                parts[splitCount++] = cleanedInput;
            }
        } else {
            // 如果没有第二个 '-' 符号，剩余部分视为一个整体
            parts[splitCount++] = cleanedInput;
        }

        // 修剪数组到实际分割的数量
        String[] result = new String[splitCount];
        System.arraycopy(parts, 0, result, 0, splitCount);
        return Arrays.asList(result);
    }
}
